package com.byb.server.service.impl;

import com.byb.server.service.MildCaseService;
import com.byb.server.domain.MildCase;
import com.byb.server.repository.MildCaseRepository;
import com.byb.server.repository.search.MildCaseSearchRepository;
import com.byb.server.service.dto.MildCaseDTO;
import com.byb.server.service.mapper.MildCaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing MildCase.
 */
@Service
@Transactional
public class MildCaseServiceImpl implements MildCaseService{

    private final Logger log = LoggerFactory.getLogger(MildCaseServiceImpl.class);

    private final MildCaseRepository mildCaseRepository;

    private final MildCaseMapper mildCaseMapper;

    private final MildCaseSearchRepository mildCaseSearchRepository;
    public MildCaseServiceImpl(MildCaseRepository mildCaseRepository, MildCaseMapper mildCaseMapper, MildCaseSearchRepository mildCaseSearchRepository) {
        this.mildCaseRepository = mildCaseRepository;
        this.mildCaseMapper = mildCaseMapper;
        this.mildCaseSearchRepository = mildCaseSearchRepository;
    }

    /**
     * Save a mildCase.
     *
     * @param mildCaseDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public MildCaseDTO save(MildCaseDTO mildCaseDTO) {
        log.debug("Request to save MildCase : {}", mildCaseDTO);
        MildCase mildCase = mildCaseMapper.toEntity(mildCaseDTO);
        mildCase = mildCaseRepository.save(mildCase);
        MildCaseDTO result = mildCaseMapper.toDto(mildCase);
        //mildCaseSearchRepository.save(mildCase);
        return result;
    }

    /**
     *  Get all the mildCases.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<MildCaseDTO> findAll(Pageable pageable) {
        log.debug("Request to get all MildCases");
        return mildCaseRepository.findAll(pageable)
            .map(mildCaseMapper::toDto);
    }

    /**
     *  Get one mildCase by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public MildCaseDTO findOne(Long id) {
        log.debug("Request to get MildCase : {}", id);
        MildCase mildCase = mildCaseRepository.findOne(id);
        return mildCaseMapper.toDto(mildCase);
    }

    /**
     *  Delete the  mildCase by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete MildCase : {}", id);
        mildCaseRepository.delete(id);
        //mildCaseSearchRepository.delete(id);
    }

    /**
     * Search for the mildCase corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<MildCaseDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of MildCases for query {}", query);
        Page<MildCase> result = mildCaseSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(mildCaseMapper::toDto);
    }
}
